package priv.yiyuan.example.composite;

import java.util.ArrayList;
import java.util.List;

/**
 * 文件系统文件夹
 *
 * 组合模式 组合节点
 *
 * @author YiYuan
 * @data 2025/8/28
 * @apoNote
 */
public class Folder implements FileSystemComponent {

    private String name;

    private List<FileSystemComponent> children = new ArrayList<>();

    public Folder(String name) {
        this.name = name;
    }

    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public long getSize() {
        long totalSize = 0;
        for (FileSystemComponent component : children) {
            totalSize += component.getSize();
        }
        return totalSize;
    }

    @Override
    public void display(String indent) {
        System.out.println(indent + "+ Folder: " + name + " (文件大小: " + getSize() + "KB)");
        for (FileSystemComponent component : children) {
            component.display(indent + "  ");
        }
    }

    @Override
    public void add(FileSystemComponent component) {
        children.add(component);
    }

    @Override
    public void remove(FileSystemComponent component) {
        children.remove(component);
    }

    @Override
    public FileSystemComponent getChild(int i) {
        return children.get(i);
    }

    @Override
    public void accept(FileSystemVisitor visitor) {
        visitor.visitFolder(this);
        for (FileSystemComponent component : children) {
            component.accept(visitor); // 递归传递给子组件接受访问者
        }
    }
}
